using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Engine;

namespace Game.Plugin.MyShopPlugin
{
    public class MyShopConfigCmd : AbstractProcessCmd
    {
        public override string Cmd => "n";
        public override string Introduce => "/n    - 商店配置命令";
        public override int AuthLevel => 10; // 权限等级10
        public override DisplayType Display => DisplayType.All;

        public void SendConfigHelpMessage()
        {
            string message = "\n/n on    - 开启商店告示牌编辑文字模式\n/n off    - 关闭商店告示牌编辑文字模式\n/n n    - 开启查看商店告示牌配置模式\n/n f    - 关闭查看商店告示牌配置模式\n/n don    - 开启删除商店模式(点击任意告示牌删除)\n/n doff   - 关闭删除商店模式\n/n <商店名称> <buy/sell> <物品ID> <数量> <价格>\n-配置商店（使用指令后点击任意告示牌）\n/n <商店名称>    - 删除指定商店\n<<<======【商店配置】MyShop配置系统======>>>";
            SendMessage("商店配置系统", message);
        }

        public void SendMsgPack(ComponentPlayer componentPlayer, string text)
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public override void ProcessCmd()
        {
            if (GameManager.Project == null)
            {
                SendMessage("<c=red>主系统</c>", "你需要先开始游戏");
                return;
            }

            if (m_isTerminal)
            {
                SendMessage("<c=red>主系统</c>", "控制台无法使用");
                return;
            }

            if (m_messageDatas.Length < 2)
            {
                SendConfigHelpMessage();
                return;
            }

            string playerGuid = m_client.GUID.ToString();

            switch (m_messageDatas[1])
            {
                case "on":
                    MyShopData.SetPlayerEditMode(playerGuid, true);
                    SendMessage("<c=green>商店</c>", "已开启商店告示牌编辑文字模式");
                    break;

                case "off":
                    MyShopData.SetPlayerEditMode(playerGuid, false);
                    SendMessage("<c=red>商店</c>", "已关闭商店告示牌编辑文字模式");
                    break;

                case "n":
                    MyShopData.SetPlayerViewMode(playerGuid, true);
                    SendMessage("<c=green>商店</c>", "已开启查看商店告示牌配置模式");
                    break;

                case "f":
                    MyShopData.SetPlayerViewMode(playerGuid, false);
                    SendMessage("<c=red>商店</c>", "已关闭查看商店告示牌配置模式");
                    break;

                case "don":
                    MyShopData.SetPlayerDeleteMode(playerGuid, true);
                    SendMessage("<c=green>商店</c>", "已开启删除商店模式，点击告示牌可删除对应的商店配置");
                    break;

                case "doff":
                    MyShopData.SetPlayerDeleteMode(playerGuid, false);
                    SendMessage("<c=red>商店</c>", "已关闭删除商店模式");
                    break;

                default:
                    if (m_messageDatas.Length == 2)
                    {
                        // 删除商店命令
                        string shopName = m_messageDatas[1];
                        if (MyShopPlugin.MyShopConfigs.ContainsKey(shopName))
                        {
                            MyShopPlugin.MyShopConfigs.Remove(shopName);
                            SaveShopConfig();
                            SendMessage("<c=green>商店</c>", $"已删除商店：{shopName}");
                        }
                        else
                        {
                            SendMessage("<c=red>商店</c>", $"商店不存在：{shopName}");
                        }
                    }
                    else if (m_messageDatas.Length == 6)
                    {
                        // 配置商店命令
                        string shopName = m_messageDatas[1];
                        string shopType = m_messageDatas[2];

                        if (shopType != "buy" && shopType != "sell")
                        {
                            SendMessage("<c=red>商店</c>", "商店类型必须是 buy 或 sell");
                            break;
                        }

                        if (!int.TryParse(m_messageDatas[3], out int itemId))
                        {
                            SendMessage("<c=red>商店</c>", "物品ID必须是数字");
                            break;
                        }

                        if (!int.TryParse(m_messageDatas[4], out int count) || count < 0)
                        {
                            SendMessage("<c=red>商店</c>", "数量必须是非负整数（0或正数）");
                            break;
                        }

                        if (!double.TryParse(m_messageDatas[5], out double money) || money < 0)
                        {
                            SendMessage("<c=red>商店</c>", "价格必须是非负数（0或正数）");
                            break;
                        }

                        ShopConfigMode configMode = new ShopConfigMode
                        {
                            shopName = shopName,
                            shopType = shopType,
                            itemId = itemId,
                            count = count,
                            money = money,
                            isActive = true
                        };
                        MyShopData.SetPlayerConfigMode(playerGuid, configMode);
                        SendMessage("<c=green>商店</c>", $"请点击任意告示牌开始配置商店");
                    }
                    else
                    {
                        SendConfigHelpMessage();
                    }
                    break;
            }
        }

        private void SaveShopConfig()
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(MyShopPlugin.MyShopConfigs, Newtonsoft.Json.Formatting.Indented);
                string directory = System.IO.Path.GetDirectoryName(MyShopPlugin.ConfigFilePath);
                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }
                System.IO.File.WriteAllText(MyShopPlugin.ConfigFilePath, json);
            }
            catch (System.Exception ex)
            {
                Log.Error($"保存商店配置失败：{ex.Message}");
            }
        }
    }
}